﻿using bpms.Enums;
using bpms.Models;
using bpms.Service;
using Microsoft.AspNetCore.Mvc;
using System;

namespace bpms.Controllers
{
    public class CustomerController : BaseController
    {
        private readonly CustomerService _customerService;
        public CustomerController(CustomerService customerService)
        {
            _customerService = customerService;
        }
        /// <summary>
        /// 获取所有客户列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(string searchKey)
        {
            var customers = _customerService.GetAll(searchKey);
            return View(customers);
        }

        // 新增页
        public IActionResult Add()
        {
            return View();
        }

        // 新增保存
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Add(Customer customer)
        {
            if (ModelState.IsValid)
            {  // 检查用户名是否存在
                if ( CheckLoginNameExists(UserType.Customer, customer.LoginName))
                {
                    ModelState.AddModelError("LoginName", "登录名已存在");
                    return View(customer);
                }
                _customerService.Add(customer);
                return RedirectToAction(nameof(Index));
            }
            return View(customer);
        }

        // 编辑客户
        public async Task<IActionResult> Edit(int id)
        {
            if (id == 0)
            {
                return NotFound();
            }
            // 查询买家用户
            var buyer = _customerService.GetById(id);
            if (buyer == null)
            {
                return NotFound();
            }

            return View(buyer);
        }

        // 保存编辑客户
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, Customer customer)
        {
            if (id != customer.Id)
            {
                TempData["ErrorMessage"] = "没找到用户信息";
                return NotFound();
            }
            //登录名不能修改，所以不验证
            ModelState.Remove("LoginName");
            if (ModelState.IsValid)
            {

                // 查询管理员用户
                var existabuyer = _customerService.GetById(id);
                if (existabuyer == null)
                {
                    TempData["ErrorMessage"] = "没找到用户信息";
                    return NotFound();
                }
                customer.Id = id;
                int i = _customerService.Update(customer);
                if (i > 0)
                {
                    TempData["SuccessMessage"] = "修改成功";
                }
                else
                {
                    TempData["ErrorMessage"] = "修改失败";
                }
            }
            return View(customer);
        }

        // 删除操作
        public IActionResult Delete(int id)
        {
            _customerService.Delete(id);
            return RedirectToAction("Index");
        }

        /// <summary>
        /// 检查登录名是否被使用
        /// </summary>
        /// <param name="userType"></param>
        /// <param name="loginName"></param>
        /// <returns></returns>
        private bool CheckLoginNameExists(UserType userType, string loginName)
        {
            var customer = _customerService.GetByLoginName(loginName);
            return customer != null;
        }
    }
}
